<template>
  <div class="auth-login">
    <div class="auth-login__title">
      云智易
    </div>
    <div class="auth-login__form">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="corp_id">
          <el-input type="text" v-model="form.corp_id" placeholder="请输入企业ID"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input type="text" v-model="form.email"  placeholder="请输入账户"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="sumbit">
          <el-button type="danger" @click="submitForm" class="login-submit" >{{submiting ? '登陆中..':'登录'}}</el-button>
        </el-form-item>
      </el-form>
      <div class="register-wrap">
        <router-link id="register-link" to="register" class="register">注册账号</router-link>
        <router-link id="register-link" to="fetch-password" class="forgot">忘记密码</router-link>
      </div>
    </div>
  </div>
</template>
<script>
import api from '@/api'

export default {
  name: 'login',

  data () {
    return {
      submiting: false,
      form: {
        email: '1325896386@qq.com',
        password: 'yangpu0924',
        corp_id: '100fa6af8c879a00'
      },
      rules: {
        email: [
          { required: true, message: '请输入登录帐号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        corp_id: [
          { required: true, message: '请输入企业ID', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * @argument
     * 表单提交
     */
    submitForm () {
      if (this.submiting) return
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.submiting = true
          try {
            let res = await api.login(this.form)
            window.localStorage.setItem('userId', res.data.user_id)
            window.localStorage.setItem('accessToken', res.data.access_token)
            window.localStorage.setItem('refreshToken', res.data.refresh_token)
            window.localStorage.setItem('expireIn', res.data.expire_in)
            window.localStorage.setItem('authorize', res.data.authorize)
            console.log('登陆成功, 登陆返回的信息已经存储到localStorage')
            this.$router.push('/user-bind-device')
          } catch (err) {
            console.log('登陆失败' + err.response.data.error.msg)
          }
          this.submiting = false
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.auth-login 
  width 500px
  border 1px solid #eee
  border-radius 12px
  margin 200px auto 1000px
  padding-bottom 20px
  box-shadow: 0 3px 6px rgba(0,0,0,.15)
  &__title
    font-size: 20px;
    margin 30px 0 25px
  &__form
    box-sizing border-box
    padding 0 80px
    .login-submit
      width 260px
      margin-top 5px
      border-radius 12px
    .register-wrap
      a
        color #a5a4a4
        text-decoration none
</style>

